package com.negative.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.net.ssl.HttpsURLConnection;

import com.negative.data.EventItem;
import com.negative.data.SubEventItem;

import android.annotation.SuppressLint;
import android.text.Html;
import android.util.JsonReader;
import android.util.JsonToken;

@SuppressLint("SimpleDateFormat")
public class APIHelper {
	private String USER_NAME;
	private String PASS;
	private String TOKEN;

	private ArrayList<EventItem> events;
	private boolean wait;

	public ArrayList<EventItem> getEventList() throws Exception{
		events = new ArrayList<EventItem>();
		wait = true;
		Thread t = new Thread() {
			public void run() {

				final String https_url = "https://www.tixwise.co.il/he/api.json?USER="
						+ USER_NAME
						+ "&PASS="
						+ PASS
						+ "&TOKEN="
						+ TOKEN
						+ "&VERSION=1.0&ACTION=producer_listEvents";
				try {
					URL url = new URL(https_url);
					HttpsURLConnection conn = (HttpsURLConnection) url
							.openConnection();
					JsonReader reader = new JsonReader(new InputStreamReader(
							conn.getInputStream(), "UTF-8"));
					reader.beginObject();
					reader.nextName();
					reader.skipValue();
					reader.nextName();
					reader.skipValue();
					reader.nextName();
					reader.beginObject();
					while (reader.hasNext()) {
						reader.nextName();
						EventItem event = readEvent(reader);
						if (event != null && event.getStatus().equals("EVENT_ACTIVE")) {
							events.add(event);
						}
						IOHelper.getSpecialEventsFromGoogleDoc(events, false);
					}
					reader.endObject();
					reader.close();

				} catch (Exception e) {
					wait = false;
					events = null;
				}
				wait = false;
			}
		};
		t.start();
		while (wait);
		if(events == null)
		{
			events = new ArrayList<EventItem>();
			throw new Exception();
		}
			
			
		return events;

	}

	private EventItem readEvent(JsonReader reader) throws Exception {
		long id = 0;
		String eventName = null;
		String location = null;
		String description = null;
		String picture = null;
		String map = null;
		String venue_picture = null;
		long producer_id = 0;
		String status = null;
		String status_description = null;
		String is_suspended = null;
		String sub_domain = null;
		ArrayList<SubEventItem> subEvents = null;

		reader.beginObject();
		while (reader.hasNext()) {
			String name = reader.nextName();
			if (name.equals("id") && reader.peek() != JsonToken.NULL) {
				id = reader.nextLong();
			} else if (name.equals("name") && reader.peek() != JsonToken.NULL) {
				eventName = reader.nextString();
				eventName = Html.fromHtml(eventName).toString();
			} else if (name.equals("location")
					&& reader.peek() != JsonToken.NULL) {
				location = reader.nextString();
			} else if (name.equals("description")
					&& reader.peek() != JsonToken.NULL) {
				description = reader.nextString();
			} else if (name.equals("picture")
					&& reader.peek() != JsonToken.NULL) {
				picture = reader.nextString();
			} else if (name.equals("map") && reader.peek() != JsonToken.NULL) {
				map = reader.nextString();
			} else if (name.equals("venue_picture")
					&& reader.peek() != JsonToken.NULL) {
				venue_picture = reader.nextString();
			} else if (name.equals("producer_id")
					&& reader.peek() != JsonToken.NULL) {
				producer_id = reader.nextLong();
			} else if (name.equals("status") && reader.peek() != JsonToken.NULL) {
				status = reader.nextString();
			} else if (name.equals("status_description")
					&& reader.peek() != JsonToken.NULL) {
				status_description = reader.nextString();
			} else if (name.equals("is_suspended")
					&& reader.peek() != JsonToken.NULL) {
				is_suspended = reader.nextString();
			} else if (name.equals("sub_domain")
					&& reader.peek() != JsonToken.NULL) {
				sub_domain = reader.nextString();
			} else if (name.equals("SubEvents")
					&& reader.peek() != JsonToken.NULL) {
				subEvents = new ArrayList<SubEventItem>();
				reader.beginObject();
				while (reader.hasNext()) {
					reader.nextName();
					SubEventItem  subEventItem = readSubEvent(reader);
					if (subEventItem != null)
					{
						subEvents.add(subEventItem);
					}
				}
				reader.endObject();
			} else {
				reader.skipValue();
			}
		}
		reader.endObject();
		if(subEvents.isEmpty() || eventName.contains("מנוי נגטיב"))
		{
			return null;
		}
		return new EventItem(id, eventName, location, description, picture,
				map, venue_picture, producer_id, status, status_description,
				is_suspended, sub_domain, subEvents);
	}

	private SubEventItem readSubEvent(JsonReader reader) throws Exception {
		long id = 0;
		long eventID = 0;
		String subeventName = null;
		Date start_time = null;
		Date end_time = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		reader.beginObject();
		while (reader.hasNext()) {
			String name = reader.nextName();
			if (name.equals("id") && reader.peek() != JsonToken.NULL) {
				id = reader.nextLong();
			} else if (name.equals("event_id")
					&& reader.peek() != JsonToken.NULL) {
				eventID = reader.nextLong();
			} else if (name.equals("name") && reader.peek() != JsonToken.NULL) {
				subeventName = reader.nextString();
			} else if (name.equals("start_time")
					&& reader.peek() != JsonToken.NULL) {
				start_time = dateFormat.parse(reader.nextString());
			} else if (name.equals("end_time")
					&& reader.peek() != JsonToken.NULL) {
				end_time = dateFormat.parse(reader.nextString());
			} else {
				reader.skipValue();
			}
		}
		reader.endObject();
		return SubEventItem.CreateSubEventItem(id, eventID, subeventName, start_time, end_time);
	}

	private String get_content(HttpsURLConnection con) {
		String ans = "";
		if (con != null) {

			try {

				BufferedReader br = new BufferedReader(new InputStreamReader(
						con.getInputStream(), "UTF-8"));

				String input;

				while ((input = br.readLine()) != null) {
					ans += input;
				}
				br.close();

			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}

		}
		return ans;
	}
	
	public void setCred(String user, String pass, String token) {
		USER_NAME = user;
		PASS = pass;
		TOKEN = token;
	}
}
